package io.bloom.webspider;

import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Slf4j
public class SpiderExtractor {

    public Map<String,Object> extract(String html) {


        Map<String,Object> map=new HashMap<String, Object>();

        Document document = Jsoup.parse(html);
        Elements aTags = document.select("a[href]");

        List<Item> items = new ArrayList<Item>();
        List<String> nextLinks = new ArrayList<String>();
        for (Element a : aTags) {
            String href = a.attr("abs:href");
            if (href != null) {
                String text = a.val();
                items.add(Item.of(href, text));
                nextLinks.add(href);
            }
        }
        map.put("link",items);
        map.put("nextLinks", nextLinks);

        items = new ArrayList<Item>();
        Elements imgTags = document.select("img[src]");
        for (Element a : imgTags) {
            String href = a.attr("abs:src");
            if (href != null) {
                String text = a.val();
                items.add(Item.of(href, text, "img"));
            }
        }
        map.put("img",items);

        return map;
    }

    public List<String> filter(List<Item> items, String type) {
        List<String> filtered = new ArrayList<String>();

        for (Item item : items) {
            if (type.equals(item.getType())) {
                filtered.add(item.getLink());
            }
        }
        return filtered;
    }
}
